package mi.payment.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;


/**
 * Author   : huangyi
 * Date     : 2017/8/29
 * Email    : huangyi@sunmi.com
 * Function : 获取应用信息的工具类
 */

public class VersionUtil {

    private static VersionUtil instance;

    public static VersionUtil getInstance() {
        if (instance == null) {
            instance = new VersionUtil();
        }
        return instance;
    }

    /**
     * 获取应用versionName
     */
    public String getVersionName(Context ctx) {
        PackageManager packageManager = ctx.getPackageManager();
        PackageInfo packInfo = null;
        try {
            packInfo = packageManager.getPackageInfo(ctx.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return packInfo.versionName;
    }

    /**
     * 获取应用versionCode
     */
    public String getVersionCode(Context ctx) {
        PackageManager packageManager = ctx.getPackageManager();
        PackageInfo packInfo = null;
        try {
            packInfo = packageManager.getPackageInfo(ctx.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return packInfo.versionCode + "";
    }

    /**
     * 获取应用包名
     */
    public String getPackageName(Context ctx) {
        PackageInfo packInfo = null;
        try {
            packInfo = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        String packageName = packInfo.applicationInfo.loadLabel(ctx.getPackageManager()).toString();
        return packageName;
    }

}
